package jmine.tec.scheduling.condition;

import static jmine.tec.scheduling.SchedulingMessages.CONDITION_MASTER_MET;
import static jmine.tec.scheduling.SchedulingMessages.CONDITION_MASTER_NOT_MET;
import jmine.tec.cluster.MasterIdentificator;
import jmine.tec.scheduling.ScheduledJobExecutionContext;

/**
 * Condição em que apenas o nó master em um cluster executa a tarefa.
 * 
 * @author ricardo
 */
public class OnlyMasterNodeCondition implements Condition {
    private MasterIdentificator identificator;

    /**
     * Constructor
     */
    public OnlyMasterNodeCondition() {
        // OK
    }

    /**
     * Constructor
     * 
     * @param identificator identificator
     */
    public OnlyMasterNodeCondition(MasterIdentificator identificator) {
        this.identificator = identificator;
    }

    /**
     * Setter do identificador de nó mestre.
     * 
     * @param identificator o identificador
     */
    public void setIdentificator(MasterIdentificator identificator) {
        this.identificator = identificator;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isSatisfied(ScheduledJobExecutionContext context) {
        if (this.identificator.isMaster()) {
            context.log(CONDITION_MASTER_MET.create(this.identificator.getHostname()));
            return true;
        } else {
            context.log(CONDITION_MASTER_NOT_MET.create(this.identificator.getHostname()));
            return false;
        }
    }
}
